﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace DMZS.DAL
{
    /// <summary>
    /// 数据访问类:Dlyndx_BarCode
    /// </summary>
    public partial class Dlyndx_BarCode
    {
        public Dlyndx_BarCode()
        {
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from Dlyndx_BarCode");
            strSql.Append(" where ID=@ID");
            var parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.BigInt)
            };
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(DMZS.Model.Dlyndx_BarCode model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into Dlyndx_BarCode(");
            strSql.Append("PTypeID,BarCodeValue,UNITID,Layer,RelBarCodeValue,btypeID,vchtype,vchcode,DlyOrder,state,Number,Date)");
            strSql.Append(" values (");
            strSql.Append("@PTypeID,@BarCodeValue,@UNITID,@Layer,@RelBarCodeValue,@btypeID,@vchtype,@vchcode,@DlyOrder,@state,@Number,@Date)");
            strSql.Append(";select @@IDENTITY");
            var parameters = new SqlParameter[] { new SqlParameter("@PTypeID", SqlDbType.VarChar, 50),
                    new SqlParameter("@BarCodeValue", SqlDbType.VarChar, 250),
                    new SqlParameter("@UNITID", SqlDbType.Int, 4),
                    new SqlParameter("@Layer", SqlDbType.Int, 4),
                    new SqlParameter("@RelBarCodeValue", SqlDbType.VarChar, 250),
                    new SqlParameter("@btypeID", SqlDbType.VarChar, 50),
                    new SqlParameter("@vchtype", SqlDbType.VarChar, 50),
                    new SqlParameter("@vchcode", SqlDbType.Int, 4),
                    new SqlParameter("@DlyOrder", SqlDbType.Int, 4),
                    new SqlParameter("@state", SqlDbType.Int, 4),
                    new SqlParameter("@Number", SqlDbType.VarChar, 100),
                    new SqlParameter("@Date", SqlDbType.DateTime) };
            parameters[0].Value = model.PTypeID;
            parameters[1].Value = model.BarCodeValue;
            parameters[2].Value = model.UNITID;
            parameters[3].Value = model.Layer;
            parameters[4].Value = model.RelBarCodeValue;
            parameters[5].Value = model.btypeID;
            parameters[6].Value = model.vchtype;
            parameters[7].Value = model.vchcode;
            parameters[8].Value = model.DlyOrder;
            parameters[9].Value = model.state;
            parameters[10].Value = model.Number;
            parameters[11].Value = model.Date;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt64(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DMZS.Model.Dlyndx_BarCode model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update Dlyndx_BarCode set ");
            strSql.Append("PTypeID=@PTypeID,");
            strSql.Append("BarCodeValue=@BarCodeValue,");
            strSql.Append("UNITID=@UNITID,");
            strSql.Append("Layer=@Layer,");
            strSql.Append("RelBarCodeValue=@RelBarCodeValue,");
            strSql.Append("btypeID=@btypeID,");
            strSql.Append("vchtype=@vchtype,");
            strSql.Append("vchcode=@vchcode,");
            strSql.Append("DlyOrder=@DlyOrder,");
            strSql.Append("state=@state,");
            strSql.Append("Number=@Number,");
            strSql.Append("Date=@Date");
            strSql.Append(" where ID=@ID");
            var parameters = new SqlParameter[] { new SqlParameter("@PTypeID", SqlDbType.VarChar, 50),
                    new SqlParameter("@BarCodeValue", SqlDbType.VarChar, 250),
                    new SqlParameter("@UNITID", SqlDbType.Int, 4),
                    new SqlParameter("@Layer", SqlDbType.Int, 4),
                    new SqlParameter("@RelBarCodeValue", SqlDbType.VarChar, 250),
                    new SqlParameter("@btypeID", SqlDbType.VarChar, 50),
                    new SqlParameter("@vchtype", SqlDbType.VarChar, 50),
                    new SqlParameter("@vchcode", SqlDbType.Int, 4),
                    new SqlParameter("@DlyOrder", SqlDbType.Int, 4),
                    new SqlParameter("@state", SqlDbType.Int, 4),
                    new SqlParameter("@Number", SqlDbType.VarChar, 100),
                    new SqlParameter("@Date", SqlDbType.DateTime),
                    new SqlParameter("@ID", SqlDbType.BigInt, 8) };
            parameters[0].Value = model.PTypeID;
            parameters[1].Value = model.BarCodeValue;
            parameters[2].Value = model.UNITID;
            parameters[3].Value = model.Layer;
            parameters[4].Value = model.RelBarCodeValue;
            parameters[5].Value = model.btypeID;
            parameters[6].Value = model.vchtype;
            parameters[7].Value = model.vchcode;
            parameters[8].Value = model.DlyOrder;
            parameters[9].Value = model.state;
            parameters[10].Value = model.Number;
            parameters[11].Value = model.Date;
            parameters[12].Value = model.ID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from Dlyndx_BarCode ");
            strSql.Append(" where ID=@ID");
            var parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.BigInt)
            };
            parameters[0].Value = ID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist )
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from Dlyndx_BarCode ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            var rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.Dlyndx_BarCode GetModel(long ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  top 1 PTypeID,BarCodeValue,UNITID,Layer,RelBarCodeValue,ID,btypeID,vchtype,vchcode,DlyOrder,state,Number,Date from Dlyndx_BarCode ");
            strSql.Append(" where ID=@ID");
            var parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.BigInt)
            };
            parameters[0].Value = ID;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.Dlyndx_BarCode DataRowToModel(DataRow row)
        {
            var model = new DMZS.Model.Dlyndx_BarCode();
            if (row != null)
            {
                if (row["PTypeID"] != null)
                {
                    model.PTypeID = row["PTypeID"].ToString();
                }
                if (row["BarCodeValue"] != null)
                {
                    model.BarCodeValue = row["BarCodeValue"].ToString();
                }
                if (row["UNITID"] != null && row["UNITID"].ToString() != string.Empty)
                {
                    model.UNITID = int.Parse(row["UNITID"].ToString());
                }
                if (row["Layer"] != null && row["Layer"].ToString() != string.Empty)
                {
                    model.Layer = int.Parse(row["Layer"].ToString());
                }
                if (row["RelBarCodeValue"] != null)
                {
                    model.RelBarCodeValue = row["RelBarCodeValue"].ToString();
                }
                if (row["ID"] != null && row["ID"].ToString() != string.Empty)
                {
                    model.ID = long.Parse(row["ID"].ToString());
                }
                if (row["btypeID"] != null)
                {
                    model.btypeID = row["btypeID"].ToString();
                }
                if (row["vchtype"] != null)
                {
                    model.vchtype = row["vchtype"].ToString();
                }
                if (row["vchcode"] != null && row["vchcode"].ToString() != string.Empty)
                {
                    model.vchcode = int.Parse(row["vchcode"].ToString());
                }
                if (row["DlyOrder"] != null && row["DlyOrder"].ToString() != string.Empty)
                {
                    model.DlyOrder = int.Parse(row["DlyOrder"].ToString());
                }
                if (row["state"] != null && row["state"].ToString() != string.Empty)
                {
                    model.state = int.Parse(row["state"].ToString());
                }
                if (row["Number"] != null)
                {
                    model.Number = row["Number"].ToString();
                }
                if (row["Date"] != null && row["Date"].ToString() != string.Empty)
                {
                    model.Date = DateTime.Parse(row["Date"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select PTypeID,BarCodeValue,UNITID,Layer,RelBarCodeValue,ID,btypeID,vchtype,vchcode,DlyOrder,state,Number,Date ");
            strSql.Append(" FROM Dlyndx_BarCode ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" PTypeID,BarCodeValue,UNITID,Layer,RelBarCodeValue,ID,btypeID,vchtype,vchcode,DlyOrder,state,Number,Date ");
            strSql.Append(" FROM Dlyndx_BarCode ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Dlyndx_BarCode ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            var obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            var strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby );
            }
            else
            {
                strSql.Append("order by T.ID desc");
            }
            strSql.Append(")AS Row, T.*  from Dlyndx_BarCode T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
    }
}

